package com.gitee.wsl.transform.encode

import com.gitee.wsl.transform.encode.hex.HexUtil
import com.gitee.wsl.transform.encode.hex.HexUtil.isHexNumber
import com.gitee.wsl.transform.encode.hex.HexUtil.toUnicodeHex
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue


/**
 * HexUtil单元测试
 * @author Looly
 */
class HexUtilTest {

    @Test
    fun toUnicodeHexTest() {
        var unicodeHex = toUnicodeHex('\u2001')
        assertEquals("\\u2001", unicodeHex)

        unicodeHex = toUnicodeHex('你')
        assertEquals("\\u4f60", unicodeHex)
    }

    @Test
    fun isHexNumberTest(){
            assertTrue(isHexNumber("0"))
            assertTrue(isHexNumber("002c"))

            var a = "0x3544534F444"
            assertTrue(isHexNumber(a))

            // https://gitee.com/chinabugotech/hutool/issues/I62H7K
            a = "0x0000000000000001158e460913d00000"
            assertTrue(isHexNumber(a))

            // 错误的
            a = "0x0000001000T00001158e460913d00000"
            assertFalse(isHexNumber(a))

            // 错误的,https://github.com/chinabugotech/hutool/issues/2857
            a = "-1"
            assertFalse(isHexNumber(a))
        }


    @Test
    fun formatHexTest() {
        val hex = "e8c670380cb220095268f40221fc748fa6ac39d6e930e63c30da68bad97f885d"
        val formatHex: String? = HexUtil.format(hex)
        assertEquals(
            "e8 c6 70 38 0c b2 20 09 52 68 f4 02 21 fc 74 8f a6 ac 39 d6 e9 30 e6 3c 30 da 68 ba d9 7f 88 5d",
            formatHex
        )
    }

    @Test
    fun formatHexTest2() {
        val hex = "e8c670380cb220095268f40221fc748fa6"
        val formatHex: String? = HexUtil.format(hex, "0x")
        assertEquals("0xe8 0xc6 0x70 0x38 0x0c 0xb2 0x20 0x09 0x52 0x68 0xf4 0x02 0x21 0xfc 0x74 0x8f 0xa6", formatHex)
    }


}
